package com.ms.log.logger;

import com.ms.log.appender.Appender;
import com.ms.log.appender.SimpleConsoleAppender;
import com.ms.log.context.LoggerContext;
import com.ms.log.level.Level;

/**
 * @author zdh
 */
public abstract class AbstractLogger implements Logger {

    public static final String LittleOrmLoggerName = "LittleOrmLogger";
    /**
     * 被记录日志的对象名称，比如某个类的全类名
     */
    protected String loggedName = LittleOrmLoggerName;

    protected Appender append = new SimpleConsoleAppender();
    protected Logger parentLogger;
    protected Level level = Level.INFO;
    protected LoggerContext loggerContext;


    public AbstractLogger(String loggedName){
        this.loggedName = loggedName;
    }
    public AbstractLogger(String loggedName,Appender appender){
        this.loggedName = loggedName;
        this.append = appender;
    }
    public AbstractLogger(String loggedName,Appender appender,Level level){
        this.loggedName = loggedName;
        this.append = appender;
        this.level = level;
    }
    public AbstractLogger(Appender appender,Level level){
        this.loggedName = loggedName;
        this.append = appender;
        this.level = level;
    }



    public String getLoggedName() {
        return loggedName;
    }

    public void setLoggedName(String loggedName) {
        this.loggedName = loggedName;
    }

    @Override
    public void trace(String s) {
        append(Level.TRACE,s);
    }

    @Override
    public void debug(String s) {
        append(Level.DEBUG,s);
    }

    @Override
    public void info(String s) {
        append(Level.INFO,s);
    }

    @Override
    public void warn(String s) {
        append(Level.WARN,s);
    }

    @Override
    public void error(String s) {
        append(Level.ERROR,s);
    }

    @Override
    public void trace(String s,Object... args) {
        append(Level.TRACE,s);
    }

    @Override
    public void debug(String s,Object... args) {
        append(Level.DEBUG,s);
    }

    @Override
    public void info(String s,Object... args) {
        append(Level.INFO,s);
    }

    @Override
    public void warn(String s,Object... args) {
        append(Level.WARN,s);
    }

    @Override
    public void error(String s,Object... args) {
        append(Level.ERROR,s);
    }



    @Override
    public void error(String s, Throwable e) {
        append(Level.ERROR,s);
    }

    @Override
    public boolean isTraceEnabled() {
        return level.getValue()<=Level.TRACE.getValue();
    }

    @Override
    public boolean isDebugEnabled() {
        return level.getValue()<=Level.DEBUG.getValue();
    }

    @Override
    public boolean isInfoEnabled() {
        return level.getValue()<=Level.INFO.getValue();
    }

    @Override
    public Appender getAppender() {
        return append;
    }

    @Override
    public void setParent(Logger parentLogger) {
        this.parentLogger = parentLogger;
    }

    @Override
    public void setLoggerContext(LoggerContext loggerContext) {
        this.loggerContext = loggerContext;
    }


    public Level getLevel() {
        return level;
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    /**
     * 控制当前级别日志消息是否输出，由子类决定
     * @param level
     * @param msg
     */
    protected abstract void append(Level level, String msg);



}
